package com.qianchang.microfinance.view.login;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;

import com.dm.library.http.DMOKHttpUtils;
import com.dm.library.http.HttpParams;
import com.dm.library.http.base.JsonResult;
import com.dm.library.http.callback.RequestCallBack;
import com.dm.library.utils.DMLog;
import com.dm.library.utils.FormatUtil;
import com.dm.library.utils.ToastUtil;
import com.dm.library.widgets.ClearEditText;
import com.dm.library.widgets.element.DTextView;
import com.qianchang.microfinance.R;
import com.qianchang.microfinance.global.DMApplication;
import com.qianchang.microfinance.setting.SettingTradPasswordActivity;
import com.qianchang.microfinance.utils.DMConstant;
import com.qianchang.microfinance.view.base.BaseActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 版 权: 深圳市迪蒙网络科技有限公司
 * 描 述: <忘记密码第一步>
 * 修 改 人: fengye
 * 修改时间: 2017-1-16
 */

public class ForgetActivityOne extends BaseActivity implements TextWatcher {
    @BindView(R.id.et_input_account)
    ClearEditText etInputAccount;
    @BindView(R.id.btn_forget_pwd_one_next)
    DTextView btnForgetPwdOneNext;
    private boolean isTradPwd;
    private static final String TAG = "ForgetActivityOne";

//    private AccountModel accountModel;btn_forget_pwd_one_next

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forget_pwd_one);
        ButterKnife.bind(this);
        initView();
        initData();
    }

    @Override
    protected void onResume() {
        super.onResume();
        etInputAccount.addTextChangedListener(this);
    }

    @Override
    protected void initView() {
        super.initView();
        Intent intent = getIntent();
        if (intent.hasExtra(SettingTradPasswordActivity.IS_TRAD_PWD) && intent.getBooleanExtra(SettingTradPasswordActivity.IS_TRAD_PWD, false)) {
            isTradPwd = true;
            DMLog.i(TAG, "是设置交易密码");
            setTitleName(getString(R.string.titleFindTranPassword));
        } else {
            setTitleName(getString(R.string.titleFindLoginPassword));
        }
    }

    @Override
    protected void initData() {
        super.initData();
        if (DMApplication.getInstance().getmUserLoginInfo() == null) return;
        String phone = DMApplication.getInstance().getmUserLoginInfo().getPhone();
        if (DMApplication.getInstance().isLogin() && FormatUtil.isValidPhone(phone, null)) {
            etInputAccount.setText(phone);
            etInputAccount.setDrawableVisible(false);//useless
            etInputAccount.setAlwaysHideBtn(true);
            etInputAccount.setEnabled(false);
            etInputAccount.setClearAble(false);
        } else {
            etInputAccount.setEnabled(true);
    /*        Drawable drawableLeft = ResourcesCompat.getDrawable(getResources(), R.drawable.btn_sc, null);
            if (null != drawableLeft) {
                drawableLeft.setBounds(drawableLeft.getMinimumWidth(), 0, 0, drawableLeft.getMinimumHeight());//必须设置图片大小，否则不显示
                etInputAccount.setCompoundDrawables(null, null, drawableLeft, null);
            }*/
        }
/*        accountModel = new AccountModel(this);
        etPhone.addTextChangedListener(new BaseTextWatcher() {
            @Override
            public void afterTextChanged(Editable s) {
                btnForgetPwdOneNext.setEnabled((s.length() == 11) ? true : false);
            }
        });*/
    }

    @OnClick({R.id.tv_back, R.id.btn_forget_pwd_one_next})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_back:
                ForgetActivityOne.this.finish();
                break;
            case R.id.btn_forget_pwd_one_next://下一步
                String strPhone = etInputAccount.getText().toString().trim();
                if (strPhone.length() != 11) {
                    ToastUtil.getInstant().show(ForgetActivityOne.this, getString(R.string.msg_phone_not_right));
                    return;
                }
                checkPhoneExist(strPhone);
                break;
        }
    }


    private void checkPhoneExist(final String s_phone) {
        show();
        HttpParams params = new HttpParams();
        params.put("phone", s_phone);
        params.put("type", "0");//0 需要校验 1 不需要校验手机号码是否存在
        DMOKHttpUtils.getInstance().post(getApplicationContext(), DMConstant.ApiURL.CHECK_USER_PHONE_IS_EXIST, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(JsonResult<String> response) {
                dismiss();
                if (DMConstant.Code.CODE_OK.equals(response.getCode())) {
                    Intent intent = new Intent(ForgetActivityOne.this, ForgetActivityTwo.class);
                    intent.putExtra("phone", s_phone);
                    intent.putExtra(SettingTradPasswordActivity.IS_TRAD_PWD, isTradPwd);
                    startActivity(intent);
                    finish();
                } else {
                    ToastUtil.getInstant().show(ForgetActivityOne.this, response.getDescription());
                }
            }

            @Override
            public void onFailure(String msg) {
                dismiss();
                ToastUtil.getInstant().show(ForgetActivityOne.this, msg);
            }

            @Override
            public void onConnectFailure(String msg) {
            }
        });
    }


    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    /**
     * 文本变化之后检测
     *
     * @param s
     */
    @Override
    public void afterTextChanged(Editable s) {
        checkButtonEnable();
    }

    /**
     * 检测按钮是否激活
     */
    private void checkButtonEnable() {
    /*    String s_1 = etInputAccount.getText().toString();

        if (!StringUtils.isEmptyOrNull(s_1) ) {
            btnForgetPwdOneNext.setEnabled(true);
        } else {
            btnForgetPwdOneNext.setEnabled(false);
        }*/

    }

}
